package com.jsmy.softapp.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.android.tu.loadingdialog.LoadingDailog;
import com.jsmy.softapp.presenter.BasePresenter;
import com.jsmy.softapp.service.UpVersionService;
import com.jsmy.softapp.util.ActivityManager;
import com.jsmy.softapp.util.DialogUtils;
import com.jsmy.softapp.util.MyLog;
import com.jsmy.softapp.util.ToastUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.UMShareAPI;

import butterknife.ButterKnife;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.event.LoginStateChangeEvent;
import io.reactivex.functions.Consumer;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/16.
 * Time: 11:16.
 * To change for file | settings | file and code templates.
 */

public abstract class BaseActivity<V, P extends BasePresenter<V>> extends AppCompatActivity {
    public String TAG;
    protected P presenter;
    public LoadingDailog loadingDailog;
    public RxPermissions rxPermissions;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        TAG = getClass().getSimpleName();
        ButterKnife.bind(this);
        loadingDailog = new LoadingDailog.Builder(this).setMessage("加载中").setCancelable(false).setCancelOutside(false).create();
        ActivityManager.getInstance().addActicity(this);
        JMessageClient.registerEventReceiver(this);
        presenter = createPresenter();
        presenter.attachView((V) this);
        ARouter.getInstance().inject(this);
        initView();
        initData();
        rxPermissions = new RxPermissions(this);
        MyLog.showLog(TAG, "onCreate - " + TAG);
    }

    protected abstract int getLayoutId();

    protected abstract P createPresenter();

    public void onEventMainThread(LoginStateChangeEvent event) {
        LoginStateChangeEvent.Reason reason = event.getReason();//获取变更的原因
        cn.jpush.im.android.api.model.UserInfo myInfo = event.getMyInfo();//获取当前被登出账号的信息
        MyLog.showLog(TAG, "" + reason + "  " + myInfo);
        switch (reason) {
            case user_password_change:
                //用户密码在服务器端被修改
                break;
            case user_logout:
                //用户换设备登录
                DialogUtils.showLogOutDialog(this);
                break;
            case user_deleted:
                //用户被删除
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        MyLog.showLog(TAG, "onDestroy - " + TAG);
        // 删除集合存储记录
        ActivityManager.getInstance().removeActivity(this);
        JMessageClient.unRegisterEventReceiver(this);
        presenter.detachView();
        loadingDailog.dismiss();
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    protected abstract void initView();

    protected abstract void initData();

    public void goToDownLoadService(final BaseActivity context, final String url){
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean){
                            Intent intent = new Intent(context, UpVersionService.class);
                            intent.putExtra("url", url);
                            startService(intent);
                        }else {
                            ToastUtil.showLong(context, "缺少权限，请授予SD卡的使用权限！");
                        }
                    }
                });
    }

//    // 上次点击时间点
//    private long lastClickTime;
//
//    //判断事件出发时间间隔是否超过预定值
//    public boolean isFastDoubleClick() {
//        long time = System.currentTimeMillis();
//        long timeD = time - lastClickTime;
//        if (0 < timeD && timeD < 3000) {
//            return true;
//        }
//        lastClickTime = time;
//        return false;
//    }
//
//    //防止连续点击
//    public void startActivity(Intent intent) {
//        if (isFastDoubleClick()) {
//            MyLog.showLog(TAG, "startActivity() 重复调用");
//            return;
//        }
//        super.startActivity(intent);
//    }

}
